package com.bruce.springai.bean;

import java.time.Instant;

import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Document(collection = "user_chat_messages")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserChatMessage {
    @Id private String id;
    private String conversationId;  // 会话ID
    private String role;            // 角色（USER/ASSISTANT）
    private String content;         // 消息内容
    private Instant timestamp;     // 时间戳

    public UserChatMessage(String conversationId, Message message,Instant timestamp) {
        this.conversationId = conversationId;
        this.role = message.getMessageType().name().toLowerCase();
        this.content = message.getText();
        this.timestamp = timestamp ;
    }

    public Message toMessage() {
        return switch (role) {
            case "user" -> new UserMessage(content);
            case "assistant" -> new AssistantMessage(content);
            default -> throw new IllegalArgumentException("Unsupported role: " + role);
        };
    }
}
